<html>

	<head>
	    <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
		<meta name="apple-mobile-web-app-capable" content="yes"/>
		<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
 	
		<script type="text/javascript" src="plugin/jquery/jquery-1.5.2.min.js" ></script>
		<script type="text/javascript" src="js/DependencyLoader.js" ></script>
	</head>
	
	<body>
		<div id="framerate" style="position: absolute;" ></div>
		<div id="imageBucket">
		</div>
		<canvas id="canvas" />
		
		
		<script type="text/javascript">
		include([
		         'renderers.All'
		         , 'com.unknown.GameWindow'
		         , 'EndlessGrid'
		         , 'InputManager'
		         , 'controller.AnalogStick'], 
			function(){
				canvas.width = 1000;
				canvas.height = 1000;
				var gameWindow = null;
				try{
					gameWindow = new com.unknown.GameWindow(canvas, imageBucket, null);

				gameWindow.start();
				var DEGREES_TO_RADIANS = Math.PI / 180.0;
				
				MouseDraw = function(){
					this.history = [];
					this.down = false;
					this.start = {x:0, y:0};
					this.active = 0;
					this.activeNode = null;
				};
				MouseDraw.prototype = {
					render : function(gw){
						var c = gw.c;
						var remove = [];
						var history = this.history;
						history.forEach(function(item, index){
							c.beginPath();
							c.strokeStyle = "cyan";
							c.lineWidth = 6;
							c.arc(item.x, item.y, item.size, 0, Math.PI*2, true);
							c.stroke();
							
							item.size--;
							if( item.size == 0 ){
								remove.push( index );
							}
						});
						remove.forEach(function(item){
							history.shift();
						});
					},
					hit : function(x, y){
						this.history.push({x:x, y:y, size:30});
					},
					intersects : function(){ return false; }
					,setActive : function(value){}
				};
							
				var md = new MouseDraw();
				
				//gameWindow.entities["MouseDraw"] = md;
				gameWindow.renderers["standard"] = new StandardRenderer()
				gameWindow.entities["grid.selection"] = new HexGrid();
				//gameWindow.entities["grid"] = md.node;
							
				var inputManager = new InputManager();
				if( 'createTouch' in document ){
					new TouchInputDevice(inputManager, canvas);
				}else{
					new MouseInputDevice(inputManager);	
				}
				
				var pointMode = {
					current : null,
					onMove : function(e){
						if( this.current && e.bId )
							this.current.reset(e.x, e.y);
					},
					onButtonDown : function(e){
						this.current = new Point(e.x, e.y);
						gameWindow.entities.push( this.current );
					},
					onButtonUp : function(e){
						if( this.current ){
							this.current.reset(e.x, e.y)
						}
						this.current = null;
					}				
				};
				var linesMode = {
					current : null,
					onMove : function(e){
						if(e.bId && this.current){
							this.current.reset(e.x, e.y);
						}
					},
					onButtonDown : function(e){
						this.current = new Line({x:e.x, y:e.y},{x:e.x, y:e.y});
						gameWindow.entities.push( this.current );
					},
					onButtonUp : function(e){
						if( this.current ){
							this.current.reset(e.x, e.y);
							this.current = null;
						}
					}
				};
				var triangleMode = {
						current : null,
						points  : 0,
						start   : null,
						onMove : function(e){
							//if(this.current){
							//	var sX = this.start.x;
							//	var sY = this.start.y;
							//	this.current.move(e.x-sX, e.y-sY);
							//}
						},
						onButtonDown : function(e){
							this.start = {x:e.x, y:e.y};
							if( !this.points ){
								this.current = new Point(e.x, e.y);
								this.points = 1;
							}else if( this.points == 1 ){
								gameWindow.entities.pop();
								this.current = new Line(this.current, {x:e.x, y:e.y});
								this.points++;
							}else if( this.points == 2 ){
								gameWindow.entities.pop();
								this.current = new Triangle(this.current.p1, this.current.p2, new Point(e.x, e.y));
								this.points = 0;
							}else{
								this.points = 0;
							}
							gameWindow.entities.push( this.current );
						},
						onButtonUp : function(e){
						}	
				};			
				var selectMode = {
					current : null,
					start   : null,
					onMove  : function(e){
						if(e.bId && this.current){
							var sX = this.start.x;
							var sY = this.start.y;
							this.current.move(e.x-sX, e.y-sY);
						}
					},
					onButtonDown : function(e){
						this.start = {x:e.x, y:e.y};
						for(var member in gameWindow.entities){
							try{
								var entity = gameWindow.entities[member];
								var intersection = entity.intersects(e);
								if(!this.current && entity && intersection ){
									entity = intersection.pop();
									this.current = entity;
									entity.setActive(true);
								}else{
									entity.setActive(false);
								}
							}catch(ex){
								console.error(ex);
							}
						}
					},
					onButtonUp : function(e){
						this.current = null;
					}
				};
				var intersectMode = {
					current : null,
					start   : null,
					shapes  : [],
					onMove  : function(e){
						if(e.bId && this.current){
							var sX = this.start.x;
							var sY = this.start.y;
							this.current.move(e.x-sX, e.y-sY);
						}
					},
					buildIntersections : function(shape1, shape2){
						var l1 = shape1.lines();
						var l2 = shape2.lines();
						var points = [];
						for(var i=0; i < l1.length; i++){
							var line1 = l1[i];
							for(var z=0; z < l2.length; z++){
								var line2 = l2[z];
								var point = Line.intersection(line1, line2);
								if( line2.intersects(point) && line1.intersects(point) ){
									points.push(point);
								}
							}
						}
						
					},				
					onButtonDown : function(e){
						this.start = {x:e.x, y:e.y};
						for(var member in gameWindow.entities){
							try{
								var entity = gameWindow.entities[member];
								var intersection = entity.intersects(e);
								if(!this.current && entity && intersection ){
									entity = intersection.pop();
									this.current = entity;
									entity.setActive(true);
								}else{
									entity.setActive(false);
								}
							}catch(ex){
								console.error(ex);
							}
						}
					},
					onButtonUp : function(e){
						this.current = null;
					}	
				};
				
				{
					gameWindow.entities["DrawPoints"] = Quad.square({x:5, y:5}, 30);
					gameWindow.entities["DrawPoints"].onHit = function(e){
						inputManager.addListener('paint', pointMode);
					};
					gameWindow.entities["DrawLines"] = Quad.square({x:5, y:40}, 30);
					gameWindow.entities["DrawLines"].onHit = function(e){
						inputManager.addListener('paint', linesMode);
					};
					
					gameWindow.entities["Triangle"] = Quad.square({x:5, y:75}, 30);
					gameWindow.entities["Triangle"].onHit = function(e){
						inputManager.addListener('paint', triangleMode);
					};
					
					gameWindow.entities["Select"] = Quad.square({x:5, y:110}, 30);
					gameWindow.entities["Select"].onHit = function(e){
						inputManager.addListener('paint', selectMode);
					};
				}
							
				inputManager.addListener('hittest', {
					onMove : function(e){
						if(e.bId){
							md.hit(e.x, e.y);
						}
						if(e.bId){
							iterate(gameWindow.entities, function(member){
								try{
									var entity = gameWindow.entities[member];
									var intersection = entity.intersects ? entity.intersects(e) : null;
									if( entity && intersection ){
										entity = intersection.pop();
										entity.setActive(true);
										md.activeNode = entity;
										return true;
									}
								}catch(ex){
									console.error(ex);
								}
							})
						}
					},
					onButtonDown : function(e){
						md.hit(e.x,e.y);
						iterate(gameWindow.entities, function(member){
							try{
								var entity = gameWindow.entities[member];
								var intersection = entity.intersects ? entity.intersects(e) : null;
								if( entity && intersection ){
									entity = intersection.pop();
									entity.setActive(true);
									if( entity.onHit ) entity.onHit(e);
									
									md.activeNode = entity;
									return true;
								}
							}catch(ex){
								console.error(ex);
							}
						});
					},
					onButtonUp : function(e){
						md.down = false;
						iterate(gameWindow.entities, function(member){
							try{
								var entity = gameWindow.entities[member];
								entity.setActive(false);
								md.activeNode = null;
							}catch(ex){
								console.error(ex);
							}
						});
					}
				});
				}catch( ex ) { alert(ex); }
				
		})
		</script>
		
	</body>
</html>